﻿using System.Windows;
using System.Windows.Interactivity;
using DevExpress.Xpf.Grid;

namespace Loki.UI.Wpf
{
    internal class TreeListIsChangedBehaviour : Behavior<TreeListControl>
    {
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.Loaded += AssociatedObject_Loaded;
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.Loaded -= AssociatedObject_Loaded;
        }

        private void AssociatedObject_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            TreeListView L_View = AssociatedObject.View as TreeListView;
            if (L_View != null && L_View.AllowEditing)
            {
                var L_AddedStyle = AssociatedObject.FindResource("IsChangedBackgroundStyle") as Style;
                if (L_AddedStyle != null)
                {
                    if (L_View.RowStyle == null)
                    {
                        L_View.RowStyle = L_AddedStyle;
                    }
                    else
                    {
                        L_View.RowStyle.Merge(L_AddedStyle);
                    }
                }
            }
        }
    }
}